package com.shiyun.generator;

import com.shiyun.model.MainTemplateConfig;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class DynamicGenerator {

    public static void doGenerator(String inputPath,String outputPath,Object dataModel) throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_32);
        File inputFile = new File(inputPath);
        File templateDir = inputFile.getParentFile();
        configuration.setDirectoryForTemplateLoading(templateDir);
        configuration.setDefaultEncoding("utf-8");
        String templateName = inputFile.getName();
        Template template = configuration.getTemplate(templateName);
        Writer out = new FileWriter(outputPath);
        template.process(dataModel,out);
        out.close();
    }
    public static void main(String[] args) throws IOException, TemplateException {

        MainTemplateConfig mainTemplateConfig = new MainTemplateConfig();
        mainTemplateConfig.setAuthor("ShiYun");
        mainTemplateConfig.setOutputText("求和结果:");
        String projectPath = System.getProperty("user.dir"); //获取当前项目根目录
        String inputPath = projectPath + File.separator + "src/main/resources/templates/MainTemplate.java.ftl"; //设置模版路径
        String outputPath = projectPath + File.separator + "MainTemplate.java";
        doGenerator(
                inputPath,
                outputPath,
                mainTemplateConfig
        );
    }
}
